package com.igeek.boot.controller;

import com.igeek.boot.entity.Result;
import com.igeek.boot.entity.User;
import com.igeek.boot.exception.noLoginException;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @package com.igeek.boot.controller
 * @Description 参数注解
 * @Author LittlePunk [296526219@qq.ccom]
 * @Version 2022.1
 * @Date 2023/12/6 16:15
 */
@Controller
@Slf4j
public class ParamController {
    /**
     * 1.@PathVariable 路径变量
     * 接收在请求的url中的数据，{key}占位符 与 @PathVariable注解中的name="key" 一致
     *
     * Postman测试，测试地址：http://localhost:8080/find/100/李四
     *
     * 使用场景：通过ID查询数据，指定ID删除数据
     */
    @GetMapping("/find/{id}/{name}")
    @ResponseBody
    public Map<String,Object> find(
            @PathVariable(name = "id",required = true) Integer id,
            @PathVariable(name = "name",required = false) String name,
            @PathVariable Map<String,Object> pv

    ){
        Map<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("name",name);
        map.put("pv",pv);
        return map;
    }

    /**
     * 2.@RequestParam 获取请求参数   前端 --->  数据  ----> 后端
     * 方式一：表单提交  <input type="text" name="username" value="张三" />
     * Postman测试  测试地址： http://localhost:8080/add  POST方式
     * Body选项卡 -> form-data -> 填写信息（text、file）
     *
     * 方式二：携带在请求的url中   url?name=zs&age=10&hobby=music&hobby=code
     * Postman测试  测试地址： http://localhost:8080/add?name=zs&age=10&hobby=music&hobby=code  POST方式
     * Params选项卡 -> Query Params -> 填写信息
     */
    @PostMapping("add")
    @ResponseBody
    public Map<String,Object> add(
            @RequestParam(value = "name",required = false,defaultValue = "") String name,
            @RequestParam(value = "age",required = false,defaultValue = "18") Integer age,
            @RequestParam(value = "hobby",required = false,defaultValue = "")String[] hobby,
            @RequestParam(value = "pic")MultipartFile pic,
            @RequestParam Map<String,Object> pv
            ){
        Map<String,Object> map = new HashMap<>();
        map.put("name" , name);
        map.put("age" , age);
        map.put("hobby" , hobby);
        map.put("pic" , pic.getOriginalFilename());
        map.put("pv" , pv);
        return map;
    }

    /**
     * @ResponseBody 处理响应结果，将java对象转换成Json数据
     * @RequestBody 处理请求数据，将Json数据格式，解析成为java对象
     * @param user
     * @return
     */
    @PostMapping(value="login")
    @ResponseBody
    public User login(@RequestBody User user){
        user.setPwd("123456");
        //Map<String, Object> map = new HashMap<>();
        //map.put("id",user.getId());
        //map.put("name",user.getName());
        //map.put("age",user.getAge());
        //map.put("pwd",user.getPwd());
        //log.info("map:{}",map);
        return user;
    }
    //@ResponseBody 将Java对象处理成JSON格式，响应结果
    //@RequestBody  将JSON格式的请求数据，解析成Java对象
    //GET ？ POST ？ 请求，能够接收到@RequestBody数据

    //@RequestHeader 获取请求头

    @GetMapping("header")
    @ResponseBody
    public Map<String,Object> testRequestHeader(
            @RequestHeader(value="User-Agent",required = true) String userAgent,
            @RequestHeader(value = "Host",required = true) String host,
            @RequestHeader(value = "token",required = false,defaultValue = "") String token
    ){
        HashMap<String, Object> map = new HashMap<>();
        map.put("User-Agent",userAgent);
        map.put("Host",host);
        map.put("token",token);
        return map;
    }

    //@CookieValue 获取Cookie值

    @GetMapping("cookie")
    @ResponseBody
    public Map<String,Object> testCookieValue(
            @CookieValue(value = "username",required = false,defaultValue = "")String username
    ){
        HashMap<String, Object> map = new HashMap<>();
        map.put("username",username);
        return map;
    }

    //@RequestAttribute 获取request域属性

    //@GetMapping("setAttr")
    //public String setAttr(
    //        HttpServletRequest request,
    //        Model model,
    //        ModelMap modelMap
    //){
    //    request.setAttribute("requestKey","aaa");
    //    model.addAttribute("modelRequest","bbb");
    //    modelMap.addAttribute("modelMapKey","ccc");
    //    return "forward:getAttr";
    //}
    //
    //@GetMapping("getAttr")
    //@ResponseBody
    //public Map<String,Object> getAttr(
    //        @RequestParam("requestKey") String requestKey,
    //        @RequestParam("modelRequest") String modelRequest,
    //        @RequestParam("modelMapKey") String modelMapKey
    //){
    //    HashMap<String, Object> map = new HashMap<>();
    //    map.put("requestKey",requestKey);
    //    map.put("modelRequest",modelRequest);
    //    map.put("modelMapKey",modelMapKey);
    //    return map;
    //}

    /**
     * 6.@RequestAttribute 获取request域属性
     * 请求作用域、会话作用域、上下文作用域、当前页作用域
     *
     * 使用场景：前后端不分离
     *
     * 6.1 发送请求数据
     */
    @GetMapping("setAttr")
    public String setAttr(
            HttpServletRequest request,
            Model model,
            ModelMap modelMap
    ){
        request.setAttribute("requestKey" , "aaa");
        model.addAttribute("modelKey" , "bbb");
        modelMap.addAttribute("modelMapKey" , "ccc");
        //请求转发，将数据携带至下一个请求
        return "forward:getAttr";
    }

    /**
     * 6.@RequestAttribute 获取request域属性
     * 6.2 接收请求数据
     */
    @GetMapping("getAttr")
    @ResponseBody
    public Map<String,Object> getAttr(
            @RequestAttribute("requestKey") String requestKey,
            @RequestAttribute("modelKey") String modelKey,
            @RequestAttribute("modelMapKey") String modelMapKey
    ){
        Map<String,Object> map = new HashMap<>();
        map.put("requestKey" , requestKey);
        map.put("modelKey" , modelKey);
        map.put("modelMapKey" , modelMapKey);
        return map;
    }

    //@MatrixVariable 矩阵变量    /userList.jsp;jessionId=xxxxx
    //@ModelAttribute 请求域中变量


    @GetMapping("noLogin")
    public Result noLogin(){
        //先测试全局异常处理
        //int i = 1/0;
        //测试自定义异常
        //if(1>0){
        //    throw new noLoginException("自定义异常测试成功");
        //}
        return null;
    }
}
